package echat.future.cn.echat.home

import android.content.Intent
import android.os.Bundle
import com.bumptech.glide.Glide
import com.zhusx.core.app._BaseAppCompatActivity
import com.zhusx.core.imp.SimpleRecyclerAdapter
import com.zhusx.core.interfaces.IHttpResult
import com.zhusx.core.network.HttpRequest
import echat.future.cn.echat.R
import echat.future.cn.echat.home.bean.HomeEntity
import echat.future.cn.echat.home.network.LoadData
import echat.future.cn.echat.home.network.SimpleHttpListener
import kotlinx.android.synthetic.main.activity_search_result.*

/**
 *
 * Author  zhusx
 * Email   327270607@qq.com
 * Create  2018/9/17 21:45
 */

class SearchResultActivity : _BaseAppCompatActivity() {
    private lateinit var adapter: SimpleRecyclerAdapter<HomeEntity>
    private var key: String? = null
    private lateinit var loadData: LoadData<List<HomeEntity>>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search_result)
        key = intent.getStringExtra(_EXTRA_String_KEY)
        initView()
        initRequest()
    }

    private fun initRequest() {
        loadData = LoadData<List<HomeEntity>>(LoadData.Api.搜索, this)
                .apply {
                    _setOnLoadingListener(object : SimpleHttpListener<List<HomeEntity>>() {
                        override fun onLoadComplete(api: LoadData.Api, request: HttpRequest<Any>, result: IHttpResult<List<HomeEntity>>) {
                            adapter._setEmptyLayout(R.layout.layout_empty)
                            adapter._setItemToUpdate(result.data)
                        }
                    })
                }
        loadData._refreshData(key)
    }

    private fun initView() {
        tv_title.text = key
        iv_return.setOnClickListener { finish() }
        adapter = object : SimpleRecyclerAdapter<HomeEntity>(R.layout.list_item_home) {
            override fun bindViewHolder(holder: _ViewHolder, p1: Int, s: HomeEntity) {
                holder.setText(R.id.tv_price, "${s.price}币/分钟")
                holder.setText(R.id.tv_name, s.username)
                holder.setText(R.id.tv_message, s.sign)
                when (s.anchor_status ?: "") {
                    "0" -> {
                        holder.setImageRes(R.id.iv_status, R.drawable.btn_lixian)
                    }
                    "1" -> {
                        holder.setImageRes(R.id.iv_status, R.drawable.btn_zaixian)
                    }
                    else -> {
                        holder.setImageRes(R.id.iv_status, R.drawable.btn_lixian)
                    }
                }
                when (s.gj ?: "") {
                    "yg" -> {
                        holder.setImageRes(R.id.iv_gj, R.drawable.btn_guan_yin)
                    }
                    "jg" -> {
                        holder.setImageRes(R.id.iv_gj, R.drawable.btn_guan_huang)
                    }
                    "cg" -> {
                        holder.setImageRes(R.id.iv_gj, R.drawable.btn_guan_cai)
                    }
                    else -> {
                        holder.setImageRes(R.id.iv_gj, R.drawable.btn_guan_yin)
                    }
                }
                Glide.with(this@SearchResultActivity).load(s.avatar).into(holder.getView(R.id.iv_image))
                holder.rootView.setOnClickListener { startActivity(Intent(this@SearchResultActivity, AnchorHomeActivity::class.java).putExtra(_EXTRA_String_ID, "${s.id}")) }
            }
        }
        recyclerView.adapter = adapter
    }
}